package com.zyx.mysql.controller;

import com.google.common.collect.Lists;
import com.zyx.mysql.mapper.generated.UserInsertMapper;
import com.zyx.mysql.mapper.generated.UserMapper;
import com.zyx.mysql.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: ZYX
 * @Date: 2021/11/16 10:58
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInsertMapper userInsertMapper;

    @GetMapping("/getUser/{id}")
    public User getUser (@PathVariable Long id){
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @GetMapping("/batchInsert/{num}")
    public String batchInsert(@PathVariable int num){

        List<User> userList = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            User user = new User();
            user.setName("biubiu"+i);
            user.setCompany("xdclass"+i);
            userList.add(user);
        }
        List<List<User>> partition = Lists.partition(userList, 200);
        partition.forEach(users -> userInsertMapper.batchInsert(users));

        return "success";
    }

}
